package cn.wzx.controller;

import cn.wzx.entity.BlogType;
import cn.wzx.entity.Blogger;
import cn.wzx.entity.User;
import cn.wzx.service.BlogService;
import cn.wzx.service.BlogTypeService;
import cn.wzx.service.UserService;
import cn.wzx.util.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/public")
public class PublicController {

    @Autowired
    UserService userService;

    @Autowired
    BlogTypeService blogTypeService;

    @Autowired
    BlogService blogService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/addBlogType")
    @ResponseBody
    public Map<String,Object> add(@RequestBody BlogType blogType){
        System.out.println("blogtype=="+blogType);
        blogType.setNum(0);
        blogTypeService.add(blogType);
        return RespJson.pack(200,"添加成功",null);
    }

    @RequestMapping("/findAllBlogTypes")
    @ResponseBody
    public Map<String,Object> findAllBlogTypes(){
        return RespJson.pack(200,"",blogTypeService.findAll());
    }

    @RequestMapping("/findTypeById/{id}")
    @ResponseBody
    public Map<String,Object> findTypeById(@PathVariable("id")String id){
        return RespJson.pack(200,"",blogTypeService.findById(id));
    }

    @RequestMapping("/findBlogsByTypeId/{typeId}")
    @ResponseBody
    public Map<String,Object> findBlogsByTypeId(@PathVariable("typeId")String typeId){
        return RespJson.pack(200,"",blogService.findBlogsByTypeId(typeId));
    }

    @RequestMapping("/read/{blogId}")
    @ResponseBody
    public Map<String,Object> read(@PathVariable("blogId")String blogId){
        return RespJson.pack(200,"",blogService.read(blogId));
    }

    @RequestMapping("/go2Read/{blogId}")
    public String go2ReadBlog(@PathVariable("blogId")String blogId){
        return "public/read";
    }

    @ResponseBody
    @RequestMapping("/findByTitle/{title}")
    public Map<String,Object> findByTitle(@PathVariable("title") String title){
        List<Blogger> lst = blogService.findByTitle(title);
        return RespJson.pack(200,"",lst);
    }

}
